package com.itwjr.jdbc2.Test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //查询


        //注册驱动
        Class.forName("com.mysql.jdbc.Driver") ;

        //获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1";

        String user = "root";
        String psw = "1234";
        Connection connection = DriverManager.getConnection(url, user, psw);

        //定义sql语句
        String sql = "select * from user ";
        //获取statement对象,包装sql语句
        Statement statement = connection.createStatement();

        //执行select语句  返回结果,返回一个结果集
        ResultSet resultSet = statement.executeQuery(sql);
        //判断所查询的结果是否存在
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            System.out.println(id);
            String name = resultSet.getString(2);
            System.out.println(name);
            int age = resultSet.getInt(3);
            System.out.println(age);


        }

        //关闭资源
        statement.close();
        connection.close();


    }
}
